package com.threeonemeeting.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.threeonemeeting.R;
import com.threeonemeeting.model.BannerAdvFooterInfo;

public class BannerAdvFooterListAdapter extends
		ArrayAdapter<BannerAdvFooterInfo> {

	private ArrayList<BannerAdvFooterInfo> mBannerAdvFooterList = null;

	public BannerAdvFooterListAdapter(Activity activity) {
		super(activity, 0);
	}

	public void setBannerAdvFooterList(
			ArrayList<BannerAdvFooterInfo> bannerAdvFooterList) {
		mBannerAdvFooterList = bannerAdvFooterList;
		notifyDataSetChanged();
	}

	public ArrayList<BannerAdvFooterInfo> getBannerAdvFooterList() {
		return mBannerAdvFooterList;
	}

	public void add(BannerAdvFooterInfo footerInfo) {
		if (mBannerAdvFooterList == null)
			mBannerAdvFooterList = new ArrayList<BannerAdvFooterInfo>();

		mBannerAdvFooterList.add(footerInfo);
	}

	public void clear() {
		if (mBannerAdvFooterList != null) {
			mBannerAdvFooterList.clear();
		}
	}

	@Override
	public int getCount() {
		if (mBannerAdvFooterList == null)
			return 0;

		return mBannerAdvFooterList.size();
	}

	@Override
	public BannerAdvFooterInfo getItem(int position) {
		if (mBannerAdvFooterList == null)
			return null;

		if (position > mBannerAdvFooterList.size() - 1)
			return null;

		return mBannerAdvFooterList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public int getSelectedItemIndex() {
		if (mBannerAdvFooterList == null)
			return -1;

		for (int i = 0; i < mBannerAdvFooterList.size(); i++) {
			BannerAdvFooterInfo footerInfo = getItem(i);
			if (footerInfo == null)
				continue;

			if (footerInfo.bSelected == true)
				return i;
		}

		return -1;
	}

	/**
	 * 更新第N个结点的状态为选中状态
	 * 
	 * @param nIndex
	 */
	public void updateSelectedStatus(int nIndex) {
		if (mBannerAdvFooterList == null)
			return;

		if (nIndex > mBannerAdvFooterList.size() - 1)
			return;

		for (int i = 0; i < mBannerAdvFooterList.size(); i++) {
			BannerAdvFooterInfo info = mBannerAdvFooterList.get(i);
			if (info != null) {
				info.bSelected = false;
			}
		}

		BannerAdvFooterInfo info = getItem(nIndex);
		if (info != null) {
			info.bSelected = true;
		}

		notifyDataSetChanged();
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		BannerAdvFooterInfo footInfo = getItem(position);
		if (footInfo == null)
			return null;

		Activity activity = (Activity) getContext();

		View rowView = convertView;
		BannerAdvFooterImageCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.gvi_banner_adv_footer, null);
			viewCache = new BannerAdvFooterImageCache(rowView);
			rowView.setTag(viewCache);
		} else {
			viewCache = (BannerAdvFooterImageCache) rowView.getTag();
		}

		// 图片
		ImageView imageView = viewCache.getBannerAdvFooterImageView();

		if (footInfo.bSelected == true) {
			imageView.setBackgroundResource(R.drawable.cp_ball_red);
		} else {
			imageView.setBackgroundResource(R.drawable.cp_ball_gray);
		}

		return rowView;
	}

	public class BannerAdvFooterImageCache {

		private View baseView;
		private ImageView bannerAdvFooterImageView;

		public BannerAdvFooterImageCache(View baseView) {
			this.baseView = baseView;
		}

		public ImageView getBannerAdvFooterImageView() {
			if (bannerAdvFooterImageView == null) {
				bannerAdvFooterImageView = (ImageView) baseView
						.findViewById(R.id.banner_adv_footer_image);
			}
			return bannerAdvFooterImageView;
		}
	}

}
